package panel.viewSpots
{
	import assists.Lang;
	import assists.log.Log;
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.events.TransformGestureEvent;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import inerface.IPhotoRead;
	import assists.URI;
	
	public class PhotoRead extends Sprite implements IPhotoRead
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _showPicList:Array = [];
		private var _showIndex:int = 1;
		private var _index:int;
		private var _picDataList:Array;
		private var _lock:Boolean;
		
		private static const DIR_LEFT:int = -1;
		private static const DIR_RIGHT:int = 1;

		private var _detail:PhotoReadDetail;
		private var _photoContainer:Sprite;
		private var _moveDir:int;
		public function PhotoRead()
		{
			super();
		}
		
		public function init(stage:Stage,loader:ResourceLoader,lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_picDataList = [{url:"assets/viewSpot/photoRead/1.jpg",desc:"1这张照片很好看 这里是照片介绍，谢谢 这里是介绍哦，看这里"},
				{url:"assets/viewSpot/photoRead/2.jpg",desc:"2这张照片很bu好看 这里是照片介绍，谢谢 这里是介绍哦，看这里"},
				{url:"assets/viewSpot/photoRead/3.jpg",desc:"3这张照片很bu好看 这里是照片介绍，谢谢 这里是介绍哦，看这里"},
				{url:"assets/viewSpot/photoRead/4.jpg",desc:"4这张照片很一般 这里是照片介绍，谢谢 这里是介绍哦，看这里"}
			
			];
			_loader = loader;
			_photoContainer = new Sprite();
			
			var firstPic:PhotoReadItem = new PhotoReadItem(_loader,_stage);
			_photoContainer.addChild(firstPic);
			firstPic.x = - _stage.stageWidth;
			_showPicList.push(firstPic);
			var secondePic:PhotoReadItem = new PhotoReadItem(_loader,_stage);
			_photoContainer.addChild(secondePic);
			secondePic.loadPic(_picDataList[1]);
			secondePic.x = 0;
			_showPicList.push(secondePic);
			var thirdPic:PhotoReadItem = new PhotoReadItem(_loader,_stage);
			_photoContainer.addChild(thirdPic);
			thirdPic.x = _stage.stageWidth;
			_showPicList.push(thirdPic);
			
			_showIndex = 1;
			_index = 1;
			
			_detail = new PhotoReadDetail(_stage,_loader);
			_detail.y = _stage.stageHeight;
			
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			_photoContainer.addEventListener(TouchEvent.TOUCH_BEGIN,clickHandler);
			this.addChild(_photoContainer);
			this.addChild(_detail);
			
		}
		
		private function render(dir:int):void
		{
			Log.trace("出发 滑动函数" + dir);
		   var picItem:PhotoReadItem = _showPicList[_showIndex];
		   TweenLite.to(picItem,1,{x:picItem.x + dir * _stage.stageWidth,y:0});
		   var picItem3:PhotoReadItem = _showPicList[showIndex(dir)];
		   var picItem2:PhotoReadItem = _showPicList[showIndex(-dir)];
		   picItem3.x = picItem2.x;
			_index = index(-dir);
		  var isNew:Boolean = picItem2.updatePic(_picDataList[_index].url)
			   TweenLite.to(picItem2,1,{x:picItem2.x + dir * _stage.stageWidth,y:0,onComplete:cartoonEnd,onCompleteParams:[picItem2,dir,isNew]}); 
		}
		
		private function cartoonEnd(target:PhotoReadItem,dir:int,isNew:Boolean):void
		{
			_showIndex = showIndex(-dir);
			if(!isNew)
			{
			target.loadPic(_picDataList[_index]);
				
			}
			_lock = false;
			_detail.changeData(_picDataList[_index].desc);
		}
		
		protected function swipeHandler(event:TouchEvent):void
		{
			_photoContainer.removeEventListener(TouchEvent.TOUCH_END,swipeHandler);
			if(_lock) return;
		     _lock = true;
			Log.trace("出发 滑动函数" + _moveDir + "---" + event.stageX);
			_moveDir = _moveDir - event.stageX;
			var _dir:int;
			if(_moveDir - 20 > 0)//left
			{
				_dir = DIR_LEFT;
				render(_dir);
			}else if(_moveDir + 20 < 0)
			{
				_dir = DIR_RIGHT;
				render(_dir);
			}else
			{
					if(_detail.y == stage.stageHeight)
					{
						TweenLite.to(_detail,1,{x:0,y:_detail.y - 2*_detail.height,onComplete:changeLock});
					}else
					{
						TweenLite.to(_detail,1,{x:0,y:_detail.y + 2*_detail.height,onComplete:changeLock});
					}
					_detail.changeData(_picDataList[_index].desc);
					function changeLock():void
					{
						_lock = false;
					}
			}
			
		}
		
		private function  index(value:int):int
		{
			var ind:int = _index + value;
			if(ind > _picDataList.length - 1)
			{
				ind = 0;
			}else if(ind < 0)
			{
				ind = _picDataList.length - 1;
			}
			return ind;
		}
		
		private function showIndex(value:int):int
		{
			var sIndex:int = _showIndex + value;
			if(sIndex < 0)
			{
				sIndex = 2;
			}
			else if(sIndex > 2)
			{
				sIndex = 0;
			}
			return sIndex;
		}
		
		protected function clickHandler(event:TouchEvent):void
		{
			_moveDir = event.stageX;
			_photoContainer.addEventListener(TouchEvent.TOUCH_END,swipeHandler);
		}
		
		public function clear():void
		{
			Log.trace("清理掉相应函数");
			_photoContainer.removeEventListener(TouchEvent.TOUCH_BEGIN,clickHandler);
			if(_lock) return;
			this.removeChild(_photoContainer);
			_photoContainer = null;
		}
		
	}
}